﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using KSS.Generator.Configuration;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraVerticalGrid.Rows;

namespace DbmlToClassGenerator.Control
{
    public partial class GeneratorSettings : UserControl
    {
        public GeneratorSettings()
        {
            InitializeComponent();
            pgrdGeneratorSetting.AutoGenerateRows = true;
            RepositoryItemTextEdit edit = new RepositoryItemTextEdit();
            pgrdGeneratorSetting.DefaultEditors.Add(typeof(string), edit);
        }

        private GeneratorSetting _setting;
        public GeneratorSetting Setting
        {
            get
            {
                return _setting;
            }
            set
            {
                _setting = value;
                SetupPropertyControl();
            }
        }

        private GeneratorSetting _designSetting;
        protected GeneratorSetting DesignSetting
        {
            get
            {
                return _designSetting;
            }
        }

        protected void SetupPropertyControl()
        {
            if (_setting == null)
            {
                return;
            }

            // Create Setting to Design Setting
            _designSetting = ConvertToDesignSetting(Setting);

            // bind setting to property grid control
            pgrdGeneratorSetting.SelectedObject = _designSetting;
            foreach (BaseRow row in pgrdGeneratorSetting.Rows)
            {
                pgrdGeneratorSetting.FullExpandRow(row);
            }
        }

        private GeneratorSetting ConvertToDesignSetting(GeneratorSetting setting)
        {
            GeneratorSetting designSetting = (GeneratorSetting)setting.Clone();
            ProjectSetting commonProject = new ProjectSetting();
            commonProject.Name = "Common";

            foreach (ProjectSetting projectConfig in designSetting.Projects)
            {
                foreach (PropertySetting propertyConfig in projectConfig.Properties)
                {
                    if (propertyConfig.Type == PropertyType.Global)
                    {
                        commonProject.Properties.Add(propertyConfig);
                    }
                }

                projectConfig.Properties.RemoveAll(
                    delegate(PropertySetting p)
                    {
                        return (p.Type == PropertyType.Global);
                    });
            }

            commonProject.Properties = ConfigurationHelper.RemoveDuplicateSections<PropertySetting>(commonProject.Properties);
            designSetting.Projects.Insert(0, commonProject);

            return designSetting;
        }

        public void GetValueFromDesign()
        {
            GetValueFromDesign(_setting, _designSetting);
        }

        private void GetValueFromDesign(GeneratorSetting setting, GeneratorSetting design)
        {
            GeneratorSetting designSetting = design.Clone() as GeneratorSetting;

            ProjectSetting comonProject = designSetting.Projects[0];
            PropertySetting commonProperty = null;
            designSetting.Projects.RemoveAt(0);

            ProjectSetting designProject = null;
            PropertySetting designProperty = null;

            foreach (ProjectSetting projectConfig in setting.Projects)
            {
                designProject = designSetting.Projects.Find(
                    delegate(ProjectSetting ps)
                    {
                        return (ps.Name == projectConfig.Name);
                    });

                foreach (PropertySetting propertyConfig in projectConfig.Properties)
                {
                    commonProperty = comonProject.Properties.Find(
                            delegate(PropertySetting p)
                            {
                                return (p.Name == propertyConfig.Name);
                            });

                    if (commonProperty != null)
                    {
                        propertyConfig.Value = commonProperty.Value;
                    }
                    else
                    {
                        designProperty = designProject.Properties.Find(
                            delegate(PropertySetting p)
                            {
                                return (p.Name == propertyConfig.Name);
                            });

                        if (designProperty != null)
                        {
                            propertyConfig.Value = designProperty.Value;
                        }
                    }
                }
            }
        }
    }
}
